package cn.flying.cloud.system.common.web;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.enums.RtCodeEnum;
import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.base.common.vo.layui.MenuInfo;
import cn.flying.cloud.system.common.service.CodeService;
import cn.flying.cloud.system.service.SysResService;
import cn.flying.cloud.system.user.entity.SysUserInfo;
import cn.flying.cloud.system.user.service.SysUserInfoService;


/**
 * Created with IntelliJ IDEA.
 *
 * @description: 首页请求处理类
 * @author: admin
 * @date: 2019-12-04
 * @time: 11:02 上午
 * @version: 1.0
 */
@Controller
public class IndexController {

    @Resource
    private SysResService sysResService;
    @Resource
    private SysUserInfoService sysUserInfoService;
    @Resource
    private CodeService cmCodeService;

    /**
     * 获取权限菜单列表-首页左侧导航菜单
     *
     * @return
     */
    @RequestMapping("/getMenuInfos")
    @ResponseBody
    public Rt<?> getResources() {
        String userId = Ft.Local.UserId();
        String tenantId = Ft.Local.TenantId();
        List<MenuInfo> menus = sysResService.getMenuInfos(userId, tenantId);
        return Rt.result(RtCodeEnum.R_SUCCESS, menus);
    }

    /**
     * 访问首页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"/index", "/"})
    public String index(Model model) {
        String username = Ft.Local.UserName();
        String fullName = Ft.Local.FullName();
        model.addAttribute("username", username + "/" + fullName);
        return "index";
    }

    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping("/setting/user/password")
    public String password(Model model) {
        String userId = Ft.Local.UserId();
        SysUserInfo userInfo = new SysUserInfo();
        userInfo.setUserId(userId);
        userInfo = sysUserInfoService.getOneObj(userInfo);
        model.addAttribute("user", userInfo);
        return "setting/user/password";
    }

    /**
     * 当前登录用户基本信息
     *
     * @return
     */
    @RequestMapping("/setting/user/info")
    public String info(Model model) {
        Map<String, String> genderList = cmCodeService.getCodeList("gender");
        model.addAttribute("genderList", genderList);
        String userId = Ft.Local.UserId();
        SysUserInfo userInfo = new SysUserInfo();
        userInfo.setUserId(userId);
        userInfo = sysUserInfoService.getOneObj(userInfo);
        model.addAttribute("user", userInfo);
        return "setting/user/info";
    }

    /**
     * 首页内容版块
     *
     * @return
     */
    @RequestMapping("/home/console")
    public String console() {
        return "home/console";
    }

    /**
     * 首页-IM-列表
     *
     * @return
     */
    @RequestMapping("/json/layim/getList.js")
    @ResponseBody
    public String list() {
        String userId = Ft.Local.UserId();
        SysUserInfo userInfo = new SysUserInfo();
        userInfo.setUserId(userId);
        userInfo = sysUserInfoService.getOneObj(userInfo);

        String str = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"user\": {\n" +
                "      \"username\": \"测试名称\",\n" +
                "      \"id\": \"100000\",\n" +
                "      \"status\": \"online\",\n" +
                "      \"sign\": \"测试\",\n" +
                "      \"avatar\": \"\"\n" +
                "    },\n" +
                "    \"friend\": [\n" +
                "      {\n" +
                "        \"groupname\": \"测试分组一\",\n" +
                "        \"id\": 0,\n" +
                "        \"list\": [\n" +
                "          {\n" +
                "            \"username\": \"测试1\",\n" +
                "            \"id\": \"100001\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"测试内容1\",\n" +
                "            \"status\": \"online\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试2\",\n" +
                "            \"id\": \"100001222\",\n" +
                "            \"sign\": \"测试内容2\",\n" +
                "            \"avatar\": \"\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试3\",\n" +
                "            \"id\": \"10034001\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试4\",\n" +
                "            \"id\": \"168168\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"测试内容4\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试5\",\n" +
                "            \"id\": \"777777\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"测试内容5\"\n" +
                "          }\n" +
                "        ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"groupname\": \"测试分组二\",\n" +
                "        \"id\": 1,\n" +
                "        \"list\": [\n" +
                "          {\n" +
                "            \"username\": \"测试6\",\n" +
                "            \"id\": \"121286\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"测试内容6\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试7\",\n" +
                "            \"id\": \"108101\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"微电商达人\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试8\",\n" +
                "            \"id\": \"12123454\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"测试内容8\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试9\",\n" +
                "            \"id\": \"102101\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试10\",\n" +
                "            \"id\": \"3435343\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"\"\n" +
                "          }\n" +
                "        ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"groupname\": \"测试分组三\",\n" +
                "        \"id\": 2,\n" +
                "        \"list\": [\n" +
                "          {\n" +
                "            \"username\": \"测试11\",\n" +
                "            \"id\": \"76543\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"测试内容11\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"username\": \"测试12\",\n" +
                "            \"id\": \"4803920\",\n" +
                "            \"avatar\": \"\",\n" +
                "            \"sign\": \"测试内容12\"\n" +
                "          }\n" +
                "        ]\n" +
                "      }\n" +
                "    ],\n" +
                "    \"group\": [\n" +
                "      {\n" +
                "        \"groupname\": \"测试群组一\",\n" +
                "        \"id\": \"101\",\n" +
                "        \"avatar\": \"\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"groupname\": \"测试群组二\",\n" +
                "        \"id\": \"102\",\n" +
                "        \"avatar\": \"\"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";

        return str;
    }
}
